# -*- coding: utf-8 -*-
"""
Created on Thu May  4 09:25:53 2017

@author: qiang.qian
"""

import pandas as pd
import numpy as np
import plotly.plotly as py
import plotly.graph_objs as go
import plotly.offline as local 

import datetime
today = datetime.date.today().strftime("%Y%m%d")

df = pd.read_table('b:\\may_data_order_' + today + '.txt',index_col='order_date')

# new
be_new = df.ix[:'2017-05-02']['new_user_order']
mean_new = be_new.mean()
df['mean_new'] = mean_new
df['fair_new'] = mean_new + 100;
df['ideal_new'] = mean_new + 300;

df['before_new'] = df['new_user_order']
  
df.loc[:'2017-05-02','new_user_order']= np.NAN  
df.loc['2017-05-04':,'before_new']= np.NAN  

#old
be_old = df.ix[:'2017-05-02']['old_user_order']
mean_old = be_old.mean()

df['mean_old'] = mean_old
df['fair_old'] = mean_old + 100;
df['ideal_old'] = mean_old + 300;

df['before_old'] = df['old_user_order']
  
df.loc[:'2017-05-02','old_user_order']= np.NAN  
df.loc['2017-05-04':,'before_old']= np.NAN  
      
x=df.index.values.tolist()
x_rev=x[::-1]  
  
event_new = go.Scatter(
    x=x,
    y=df['new_user_order'],
    name='活动中-新用户订单'
)

target_new = go.Scatter(
    x=x+x_rev,
    y=df['fair_new'].tolist()+df['ideal_new'].tolist(),
    fill='tozerox',
    fillcolor='rgba(231,107,243,0.2)',
    line=go.Line(color='transparent'),
    name='活动目标-新用户订单',
)

before_mean_new = go.Scatter(
    x=x,
    y=df['mean_new'],
    name='活动前平均-新用户订单'
)

before_new = go.Scatter(
    x=x,
    y=df['before_new'],
    name='活动前-新用户订单'
)

data = [before_new, before_mean_new,target_new,event_new]
layout = Layout(
    title='拿去花订单',
    paper_bgcolor='rgb(255,255,255)',
    plot_bgcolor='rgb(229,229,229)',
    xaxis=XAxis(
        gridcolor='rgb(255,255,255)',
        showgrid=True,
        showline=False,
        showticklabels=True,
        tickcolor='rgb(127,127,127)',
        ticks='outside',
        zeroline=False
    ),
    yaxis=YAxis(
        gridcolor='rgb(255,255,255)',
        showgrid=True,
        showline=False,
        showticklabels=True,
        tickcolor='rgb(127,127,127)',
        ticks='outside',
        zeroline=False
    ),
)
fig = go.Figure(data=data, layout=layout)
plot_url = local.plot(fig, filename='may_event_order.html')